package com.sky.Task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 */1 * * * *")
     public void processOutTimeOrder(){
        log.info("订单超时，系统自动取消");
       List<Orders> list = orderMapper.selectByStatusAndOrderTime(Orders.PENDING_PAYMENT, LocalDateTime.now().minusMinutes(15));
       if (list != null && list.size() > 0) {
           list.forEach(order -> {
               order.setStatus(Orders.CANCELLED);
               order.setCancelTime(LocalDateTime.now());
               order.setCancelReason("订单超时，系统自动取消");
               orderMapper.update(order);
           });
       }

     }

     @Scheduled(cron = "0 0 1 * * *")
//     @Scheduled(cron = "0 49 10 * * *")  //TODO 测试数据
    public void processInTimeOrder(){
        log.info("订单派送中,系统自动操作");
         List<Orders> list = orderMapper.selectByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().minusHours(1));
         if (list != null && list.size() > 0) {
             list.forEach(order -> {
                 order.setStatus(Orders.COMPLETED);
                 order.setDeliveryTime(LocalDateTime.now());
                 orderMapper.update(order);
             });
         }
     }



}
